package com.zrwl.poscloud.core.restapi.admin.baseInfo;

import com.alibaba.fastjson.JSONObject;
import com.zrwl.poscloud.core.annotion.AuthorityVerify.AuthorityVerify;
import com.zrwl.poscloud.core.annotion.OperationLogger.OperationLogger;
import com.zrwl.poscloud.commons.exception.ThrowableUtils;
import com.zrwl.poscloud.commons.utils.ExcelUtil;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.commons.validator.group.Delete;
import com.zrwl.poscloud.commons.validator.group.GetList;
import com.zrwl.poscloud.commons.validator.group.Insert;
import com.zrwl.poscloud.commons.validator.group.Update;
import com.zrwl.poscloud.xo.service.AdminService;
import com.zrwl.poscloud.xo.service.DeptAdminRService;
import com.zrwl.poscloud.xo.service.DeptService;
import com.zrwl.poscloud.xo.service.EmployeeService;
import com.zrwl.poscloud.xo.vo.AdminVO;
import com.zrwl.poscloud.xo.vo.DeptVO;
import com.zrwl.poscloud.xo.vo.EmployeeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * 持卡人员 controller api类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-02-09
 */
@RestController
@Api(value = "持卡人员相关接口", tags = {"持卡人员相关接口"})
@RequestMapping("/employee")
@Slf4j
public class EmployeeRestApi {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private DeptAdminRService deptAdminRService;

    @AuthorityVerify
    @ApiOperation(value = "获取持卡人员列表", notes = "获取持卡人员列表", response = String.class)
    @PostMapping("/getList")
    public JSONObject getList(@Validated({GetList.class}) @RequestBody EmployeeVO employeeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取持卡人员列表: {}", employeeVO);
        AdminVO adminVO = adminService.getMe();
        if (adminVO.isCompany()) {
            if (employeeVO.getCompanyCode() == null) {
                employeeVO.setCompanyCode(adminVO.getCompanyCode());
            }
        } else {
            return ResultUtil.errorWithJSONMessage("仅商户用户支持此功能");
        }
        employeeVO.setDeptRoleTree(deptAdminRService.getMyDeptRoleTreeCode());
        return ResultUtil.successWithJSONData(employeeService.getPageList(employeeVO));
    }
    @AuthorityVerify
    @ApiOperation(value = "获取同名持卡人员列表", notes = "获取同名持卡人员列表", response = String.class)
    @PostMapping("/getNameSakeList")
    public JSONObject getNameSakeList(@Validated({GetList.class}) @RequestBody EmployeeVO employeeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取同名持卡人员列表: {}", employeeVO);
        AdminVO adminVO = adminService.getMe();
        if (adminVO.isCompany()) {
            if (employeeVO.getCompanyCode() == null) {
                employeeVO.setCompanyCode(adminVO.getCompanyCode());
            }
        } else {
            return ResultUtil.errorWithJSONMessage("仅商户用户支持此功能");
        }
        employeeVO.setDeptRoleTree(deptAdminRService.getMyDeptRoleTreeCode());
        return ResultUtil.successWithJSONData(employeeService.getNameSakeList(employeeVO));
    }
    @AuthorityVerify
    @ApiOperation(value = "获取持卡人员列表下拉框", notes = "获取持卡人员列表下拉框", response = String.class)
    @PostMapping("/getListForSelect")
    public JSONObject getListForSelect(@Validated({GetList.class}) @RequestBody EmployeeVO employeeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取持卡人员列表下拉框: {}", employeeVO);
        if ("0".equals(employeeVO.getCompanyUid())) {
            AdminVO adminVO = adminService.getMe();
            if (adminVO.isCompany()) {
                employeeVO.setCompanyUid(adminVO.getCompanyUid());
            } else {
                return ResultUtil.errorWithJSONMessage("仅商户用户支持此功能");
            }
        } else {
            employeeVO.setCompanyUid(null);//允许跨商户搜索
        }
        return ResultUtil.successWithJSONData(employeeService.getListForSelect(employeeVO));
    }


    @AuthorityVerify
    @ApiOperation(value = "获取持卡人员列表", notes = "获取持卡人员列表", response = String.class)
    @PostMapping("/getAllList")
    public String getAllList(@Validated({GetList.class}) @RequestBody EmployeeVO employeeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取持卡人员列表: {}", employeeVO);
        AdminVO adminVO = adminService.getMe();
        if (adminVO.isCompany()) {
            if (employeeVO.getCompanyCode() == null) {
                employeeVO.setCompanyCode(adminVO.getCompanyCode());
            }
        } else {
            return ResultUtil.errorWithMessage("仅商户用户支持此功能");
        }
        return ResultUtil.successWithData(employeeService.getAllList(employeeVO));
    }
    @AuthorityVerify
    @ApiOperation(value = "获取管理员绑定持卡人列表", notes = "获取管理员绑定持卡人列表", response = String.class)
    @PostMapping("/getAdminBindEmployeeList")
    public String getAdminBindEmployeeList(@Validated({GetList.class}) @RequestBody EmployeeVO employeeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取管理员绑定持卡人列表: {}", employeeVO);
        AdminVO adminVO = adminService.getMe();
        if (!adminVO.isCompany()) {
            return ResultUtil.errorWithMessage("仅商户用户支持此功能");
        }
        employeeVO.setCompanyUid(adminVO.getCompanyUid());

        return ResultUtil.successWithData(employeeService.getAdminBindEmployeeList(employeeVO));
    }
    @AuthorityVerify
    @ApiOperation(value = "获取管理员绑定教师列表", notes = "获取管理员绑定教师列表", response = String.class)
    @PostMapping("/getAdminBindTeacherList")
    public String getAdminBindTeacherList(@Validated({GetList.class}) @RequestBody EmployeeVO employeeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取管理员绑定教师列表: {}", employeeVO);
        AdminVO adminVO = adminService.getMe();
        if (!adminVO.isCompany()) {
            return ResultUtil.errorWithMessage("仅商户用户支持此功能");
        }
        employeeVO.setCompanyUid(adminVO.getCompanyUid());

        return ResultUtil.successWithData(employeeService.getAdminBindTeacherList(employeeVO));
    }
    @AuthorityVerify
    @ApiOperation(value = "获取教师列表", notes = "获取教师列表", response = String.class)
    @PostMapping("/getTeacherInfo")
    public String getTeacherInfo(@Validated({GetList.class}) @RequestBody EmployeeVO employeeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取管理员绑定持卡人列表: {}", employeeVO);
        AdminVO adminVO = adminService.getMe();
        if (!adminVO.isCompany()) {
            return ResultUtil.errorWithMessage("仅商户用户支持此功能");
        }
        if (employeeVO.getCompanyCode() == null){
            employeeVO.setCompanyCode(adminVO.getCompanyCode());
        }
        employeeVO.setDeptRoleTree(deptAdminRService.getMyDeptRoleTreeCode());

        return ResultUtil.successWithData(employeeService.getTeacherInfo(employeeVO));
    }
    @AuthorityVerify
    @ApiOperation(value = "获取班级的教师列表", notes = "获取班级的教师列表", response = String.class)
    @PostMapping("/getGradeTeacherInfo")
    public String getGradeTeacherInfo(@Validated({GetList.class}) @RequestBody EmployeeVO employeeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取班级的教师列表: {}", employeeVO);
        AdminVO adminVO = adminService.getMe();
        if (!adminVO.isCompany()) {
            return ResultUtil.errorWithMessage("仅商户用户支持此功能");
        }
        employeeVO.setDeptRoleTree(deptAdminRService.getMyDeptRoleTreeCode());
        return ResultUtil.successWithData(employeeService.getGradeTeacherInfo(employeeVO));
    }
    @AuthorityVerify
    @ApiOperation(value = "获取班级的学生列表", notes = "获取班级的学生列表", response = String.class)
    @PostMapping("/getGradeStudentInfo")
    public String getGradeStudentInfo(@Validated({GetList.class}) @RequestBody EmployeeVO employeeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取班级的学生列表: {}", employeeVO);
        AdminVO adminVO = adminService.getMe();
        if (!adminVO.isCompany()) {
            return ResultUtil.errorWithMessage("仅商户用户支持此功能");
        }
        employeeVO.setDeptRoleTree(deptAdminRService.getMyDeptRoleTreeCode());
        return ResultUtil.successWithData(employeeService.getGradeStudentInfo(employeeVO));
    }
    @AuthorityVerify
    @OperationLogger(value = "新增持卡人员")
    @ApiOperation(value = "新增持卡人员", notes = "新增持卡人员", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody EmployeeVO employeeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("新增持卡人员: {}", employeeVO);
        AdminVO adminVO = adminService.getMe();
        if (!adminVO.isCompany()) {
            ResultUtil.errorWithMessage("仅商户用户支持此功能");
        }
        return employeeService.add(employeeVO);
    }
    @AuthorityVerify
    @OperationLogger(value = "修改持卡人员")
    @ApiOperation(value = "修改持卡人员", notes = "修改持卡人员", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody EmployeeVO employeeVO, BindingResult result) throws Exception {
        ThrowableUtils.checkParamArgument(result);
        log.info("修改持卡人员: {}", employeeVO);
        return employeeService.edit(employeeVO);
    }
    @AuthorityVerify
    @OperationLogger(value = "修改持卡人员")
    @ApiOperation(value = "修改持卡人员", notes = "修改持卡人员", response = String.class)
    @PostMapping("/editNameSake")
    public String editNameSake(@Validated({Update.class}) @RequestBody EmployeeVO employeeVO, BindingResult result) throws Exception {
        ThrowableUtils.checkParamArgument(result);
        log.info("修改持卡人员: {}", employeeVO);
        return employeeService.editNameSake(employeeVO);
    }
    @AuthorityVerify
    @OperationLogger(value = "修改人事变动")
    @ApiOperation(value = "修改人事变动", notes = "修改人事变动", response = String.class)
    @PostMapping("/editEmployeeChange")
    public String editEmployeeChange(@Validated({Insert.class}) @RequestBody EmployeeVO employeeVO, BindingResult result) throws Exception {
        ThrowableUtils.checkParamArgument(result);
        log.info("修改人事变动: {}", employeeVO);
        return employeeService.editEmployeeChange(employeeVO);
    }
    @AuthorityVerify
    @OperationLogger(value = "删除持卡人员")
    @ApiOperation(value = "删除持卡人员", notes = "删除持卡人员", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody EmployeeVO employeeVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("删除持卡人员: {}", employeeVO);
        return employeeService.delete(employeeVO);
    }
    @AuthorityVerify
    @OperationLogger(value = "获取自动生成人员编号")
    @ApiOperation(value = "获取自动生成人员编号", notes = "获取自动生成人员编号", response = String.class)
    @PostMapping("/getAutoEmployeeCode")
    public String getAutoEmployeeCode() throws Exception {
        log.info("获取自动生成人员编号" + new Date());
        return ResultUtil.successWithData(employeeService.getAutoEmployeeCode());
    }

    @AuthorityVerify
    @ApiOperation(value = "通过编号和姓名或身份证号获取持卡人信息", notes = "通过编号和姓名或身份证号获取持卡人信息", response = String.class)
    @PostMapping("/getEmployeeMesByCodeAndNameOrCertCode")
    public String getEmployeeMesByCodeAndNameOrCertCode(@Validated({GetList.class}) @RequestBody EmployeeVO employeeVO, BindingResult result){
        ThrowableUtils.checkParamArgument(result);
        log.info("通过编号和姓名或身份证号获取持卡人信息: {}", employeeVO);
        employeeVO.setDeptRoleTree(deptAdminRService.getMyDeptRoleTreeCode());
        return ResultUtil.successWithData(employeeService.getEmployeeMesByCodeAndNameOrCertCode(employeeVO));
    }
    @AuthorityVerify
    @ApiOperation(value = "通过UID获取手机端需要的详细人员展示数据", notes = "通过UID获取手机端需要的详细人员展示数据", response = String.class)
    @PostMapping("/getEmployeeDetailForMobile")
    public String getEmployeeDetailForMobile(@RequestBody EmployeeVO employeeVO){
        return ResultUtil.successWithData(employeeService.getEmployeeDetailForMobile(employeeVO));
    }
    @AuthorityVerify
    @ApiOperation(value = "通过编号和姓名或身份证号获取教师信息", notes = "通过编号和姓名或身份证号获取教师信息", response = String.class)
    @PostMapping("/getTeacherMesByCodeAndNameOrCertCode")
    public String getTeacherMesByCodeAndNameOrCertCode(@Validated({GetList.class}) @RequestBody EmployeeVO employeeVO, BindingResult result){
        ThrowableUtils.checkParamArgument(result);
        log.info("通过编号和姓名或身份证号获取教师信息: {}", employeeVO);
        employeeVO.setDeptRoleTree(deptAdminRService.getMyDeptRoleTreeCode());
        return ResultUtil.successWithData(employeeService.getTeacherMesByCodeAndNameOrCertCode(employeeVO));
    }
    @AuthorityVerify
    @ApiOperation(value = "通过编号和姓名或身份证号获取无班级教师信息", notes = "通过编号和姓名或身份证号获取教师信息", response = String.class)
    @PostMapping("/getNoGradeTeacherMesByCodeAndNameOrCertCode")
    public String getNoGradeTeacherMesByCodeAndNameOrCertCode(@Validated({GetList.class}) @RequestBody EmployeeVO employeeVO, BindingResult result){
        ThrowableUtils.checkParamArgument(result);
        log.info("通过编号和姓名或身份证号获取教师信息: {}", employeeVO);
        employeeVO.setDeptRoleTree(deptAdminRService.getMyDeptRoleTreeCode());
        return ResultUtil.successWithData(employeeService.getNoGradeTeacherMesByCodeAndNameOrCertCode(employeeVO));
    }
}